//
//  TWFXFutureWeatherView.m
//
//
//  Created by Lion User on 13-10-17.
//  Copyright (c) 2013年 Lion User. All rights reserved.
//

#import "TWFXFutureWeatherView.h"

//未来五天的温度,字符串形式
NSMutableArray * tempOfDays_str;
NSMutableArray * tempOfNighs_str;

UILabel* labelDays[5];
UILabel* labelWeeks[5];
UILabel* labelTempNight[5];
UILabel* labelTempDay[5];
UIImageView *img[5];

CGFloat heightOfScreen;


@implementation TWFXFutureWeatherView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


//一些控件不能在这里 addsubview 添加,否则重绘 draw 后就没了?
-(id)initWithFram:(CGRect)frame WeatherInfo:(TWFXStruct_AllWeatherInfo*) weather
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        self.backgroundColor = [UIColor clearColor];
        heightOfScreen = [UIScreen mainScreen].applicationFrame.size.height;
        self.weather = weather;
    }
    return self;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
 - (void)drawRect:(CGRect)rect
{

    if (self.weather.futureWeatherInfo.temp2 != nil)
    {
        [self initPoints];
    }
    else
    {
        return;
    }
    
    for(UIView *View in [self subviews])
    {

        [View removeFromSuperview];//删除此控件
        
    }

    
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    
    [self drawTend:ctx];    //折线图
    [self drawImage];       //图标
    [self drawTempOfNight]; //最低温
    [self drawTempOfDay];   //最低温
    [self drawWeek];        //日期
}



//画趋势图
-(void)drawTend:(CGContextRef) ctx
{
      
    CGContextSetStrokeColorWithColor(ctx, [[UIColor redColor] CGColor]);
    [self drawLines:ctx array:self.pointsOfDay];//画最高温折线
    
    CGContextSetStrokeColorWithColor(ctx, [[UIColor blueColor] CGColor]);
    [self drawLines:ctx array:self.pointsOfNigh];//画最低温折线
    
    
    [self drawEllipse:ctx array:self.pointsOfDay];//画最高温圆点
    [self drawEllipse:ctx array:self.pointsOfNigh];//画最低温圆点
}


//画一组线
-(void)drawLines:(CGContextRef) ctx array:(NSMutableArray*) points
{
    CGContextSetLineWidth(ctx, 3);
    
    for (int i = 0 ; i < points.count - 1; i++) {
        
        CGPoint p1 = [points[i] CGPointValue];
        CGPoint p2 = [points[i + 1] CGPointValue];
        
        [self drawLine:ctx point1:p1 point2:p2];
    }
    
    CGContextStrokePath(ctx);
}


//画一条直线
-(void)drawLine:(CGContextRef) ctx point1:(CGPoint) p1 point2:(CGPoint) p2
{
    CGContextMoveToPoint(ctx, p1.x, p1.y);
    
    CGContextAddLineToPoint(ctx, p2.x, p2.y);
    
    CGContextStrokePath(ctx);
}


//画一组实心点
-(void)drawEllipse:(CGContextRef) ctx array:(NSMutableArray*) points
{
    
    CGSize size = CGSizeMake(10, 10);
    
    CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
    
    for (int i = 0 ; i < points.count; i++)
    {     
        CGPoint point = [points[i] CGPointValue];
        CGContextFillEllipseInRect(ctx, CGRectMake(point.x - size.width/2, point.y - size.height/2, size.width, size.height));
    }
    
    CGContextStrokePath(ctx);
}


//将温度转成坐标
-(void)initPoints
{
    
    //获取未来五天的温度,字符串形式
    tempOfDays_str = [[NSMutableArray alloc] init];
    tempOfNighs_str = [[NSMutableArray alloc] init];
        
    
    if (self.weather.futureWeatherInfo.temp2 != nil)
    {
        NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"℃~"];
        
        NSArray *tempOne = [self.weather.futureWeatherInfo.temp1 componentsSeparatedByCharactersInSet: set];
        NSArray *tempTwo = [self.weather.futureWeatherInfo.temp2 componentsSeparatedByCharactersInSet: set];
        NSArray *tempThree = [self.weather.futureWeatherInfo.temp3 componentsSeparatedByCharactersInSet: set];
        NSArray *tempFour = [self.weather.futureWeatherInfo.temp4 componentsSeparatedByCharactersInSet: set];
        NSArray *tempFive = [self.weather.futureWeatherInfo.temp5 componentsSeparatedByCharactersInSet: set];

        [tempOfDays_str addObject:tempOne[0]];
        [tempOfDays_str addObject:tempTwo[0]];
        [tempOfDays_str addObject:tempThree[0]];
        [tempOfDays_str addObject:tempFour[0]];
        [tempOfDays_str addObject:tempFive[0]];
        
        [tempOfNighs_str addObject:tempOne[2]];
        [tempOfNighs_str addObject:tempTwo[2]];
        [tempOfNighs_str addObject:tempThree[2]];
        [tempOfNighs_str addObject:tempFour[2]];
        [tempOfNighs_str addObject:tempFive[2]];

    }
    else
    {
        return;
    }
    
    
    //未来五天的温度,浮点数形式
    int count = tempOfDays_str.count;
    
    CGFloat tempyOfDays_float[5];
    CGFloat tempyOfNights_float[5];
    
    int high = -300; //最高温度
    int low = 300; //最低温度
    
    //特殊处理第一天，如果是18点同步的数据，将不描绘白天的点
    tempyOfDays_float[0] = [tempOfDays_str[0] floatValue];
    tempyOfNights_float[0] = [tempOfNighs_str[0] floatValue];

    for (int i = 0; i < count; i++)
    {
        tempyOfDays_float[i] = [tempOfDays_str[i] floatValue];
        tempyOfNights_float[i] = [tempOfNighs_str[i] floatValue];
        
        if (high < tempyOfDays_float[i])
        {
            high = tempyOfDays_float[i];
        }
        
        if (low > tempyOfNights_float[i])
        {
            low = tempyOfNights_float[i];
        }
    }
    
    
    //未来五天的温度,坐标轴上Y轴的数值
    self.pointsOfDay = [[[NSMutableArray alloc] init] autorelease];
    self.pointsOfNigh = [[[NSMutableArray alloc] init] autorelease];
 
    CGFloat highPoint = heightOfScreen - 44 - 49 - 10 - 20 - 100;
    CGFloat lowPoint = 150;
    
    if (heightOfScreen == 460)
    {
        highPoint = 240;
        lowPoint = 140;
        
    }
    else if(heightOfScreen == 548)
    {
        highPoint = 300;
        lowPoint = 170;
    }
    
    
    for (int i = 0; i < count; i++)
    {
        int x = 30 + i * 60;
        
        //这里要注意,y_day_abs 这个计算出温度在有效范围内(直线上的高度)
        //因为view的Y轴是从上往下的,所以要用 highPoint - y_day_abs 计算出温度点在以highpoint为水平线的基础上的Y轴的值
        int y_day_abs = (highPoint - lowPoint) * (tempyOfDays_float[i] - low)/(high - low);
        int y_day = highPoint - y_day_abs;
        
        int y_night_abs = (highPoint - lowPoint) * (tempyOfNights_float[i] - low)/(high - low);
        int y_night = highPoint - y_night_abs;
        
        [self.pointsOfDay addObject:[NSValue valueWithCGPoint:CGPointMake(x, y_day)]];
        [self.pointsOfNigh addObject:[NSValue valueWithCGPoint:CGPointMake(x, y_night)]];
    }
    
    if (tempyOfDays_float[0] == -999 )
    {
        [self.pointsOfDay removeObjectAtIndex:0];
    }

}


//日期
-(void)drawWeek
{
    NSString *date_original = self.weather.futureWeatherInfo.date_y;
    if (date_original == nil)
    {
        return;
    }
    
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"年月日"];
    NSArray *arr = [date_original componentsSeparatedByCharactersInSet: set];
    NSString *date_str = [NSString stringWithFormat:@"%@-%@-%@ 4:00:00",arr[0], arr[1], arr[2]];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   
    //设置用string构造NSDate时的格式(和用以初始化的string的格式一一对应)
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    //设置时区
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [dateFormatter setTimeZone:GTMzone];  
  // [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    
    NSDate *date = [dateFormatter dateFromString:date_str];
    
    
    //输出周几用到,否则就是输出英文的周几
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

    //输出 周几 的格式
    [dateFormatter setDateFormat:@"EEE"];
    NSTimeInterval timeInterval = 24 * 60 * 60;
    
    for (int i = 0; i < 5; i++)
    {
        CGRect rect = CGRectMake(60 * i, 8, 60, 20);
        labelWeeks[i] = [[UILabel alloc] initWithFrame:rect];
        labelWeeks[i].backgroundColor = [UIColor clearColor];
        [labelWeeks[i] setTextColor:[UIColor whiteColor]];
        [labelWeeks[i] setTextAlignment:NSTextAlignmentCenter];
        labelWeeks[i].font = [UIFont systemFontOfSize:13];
        labelWeeks[i].text = [dateFormatter stringFromDate:[date dateByAddingTimeInterval:timeInterval * i]];
        
        [self addSubview:labelWeeks[i]];
        
        [labelWeeks[i] release];
    }
    
    
    //输出 周几 的格式
    [dateFormatter setDateFormat:@"MM/dd"];
    
    CGFloat y = heightOfScreen - 44 - 49 - 20 - 20 - 20;
    for (int i = 0; i < 5; i++)
    {
        CGRect rect = CGRectMake(60 * i, y, 60, 20);
        labelDays[i] = [[UILabel alloc] initWithFrame:rect];
        labelDays[i].backgroundColor = [UIColor clearColor];
        [labelDays[i] setTextColor:[UIColor whiteColor]];
        [labelDays[i] setTextAlignment:NSTextAlignmentCenter];
        labelDays[i].font = [UIFont systemFontOfSize:13];
        labelDays[i].text = [dateFormatter stringFromDate:[date dateByAddingTimeInterval:timeInterval * i]];
        
        [self addSubview:labelDays[i]];
        
        [labelDays[i] release];
    }
    
    [dateFormatter release];

}


//白天天气图标
-(void)drawImage
{
        
    if (self.weather.futureWeatherInfo.img2 == nil)
    {        
        return;
    }
    
    int correct = 0;
    if (self.pointsOfDay.count == 4)
    {
        correct = 60;
    }
    
    for (int i =0; i < self.pointsOfDay.count; i++)
    {
        CGRect rect = CGRectMake(60*i + correct + 10, 40, 40, 40);
        img[i] = [[UIImageView alloc] initWithFrame:rect];
        [self addSubview:img[i]];
    }
    
    [img[0] setImage:[UIImage imageNamed:self.weather.futureWeatherInfo.img3]];
    [img[1] setImage:[UIImage imageNamed:self.weather.futureWeatherInfo.img5]];
    [img[2] setImage:[UIImage imageNamed:self.weather.futureWeatherInfo.img7]];
    [img[3] setImage:[UIImage imageNamed:self.weather.futureWeatherInfo.img9]];
    
    if (self.pointsOfDay.count == 5)
    {        
        [img[4] setImage:[UIImage imageNamed:self.weather.futureWeatherInfo.img11]];
    }
    else
    {
        CGRect rect = CGRectMake(10, 40, 40, 40);
        img[4] = [[UIImageView alloc] initWithFrame:rect];
        [img[4] setImage:[UIImage imageNamed:self.weather.futureWeatherInfo.img1]];
        [self addSubview:img[4]];
    }


}


//夜间温度
-(void)drawTempOfNight
{
    for (int i = 0; i < 5; i++)
    {
        
        CGRect rect = CGRectMake( i*60, [self.pointsOfNigh[i] CGPointValue].y + 10, 60, 30);
        labelTempNight[i] = [[UILabel alloc] initWithFrame:rect];
        labelTempNight[i].backgroundColor = [UIColor clearColor];
        [labelTempNight[i] setTextColor:[UIColor whiteColor]];
        [labelTempNight[i] setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:labelTempNight[i]];
    }
    
    for (int i = 0; i < 5; i++)
    {
        
        labelTempNight[i].text = [NSString stringWithFormat:@"%@℃",tempOfNighs_str[i]];
    }
    
}


//白天温度
-(void)drawTempOfDay
{
    int correct = 0;
    if (self.pointsOfDay.count == 4)
    {
        correct = 60;
    }
    
    for (int i = 0; i < self.pointsOfDay.count; i++)
    {
        
        CGRect rect = CGRectMake( i*60 + correct, [self.pointsOfDay[i] CGPointValue].y - 10 - 30, 60, 30);
        labelTempDay[i] = [[UILabel alloc] initWithFrame:rect];
        labelTempDay[i].backgroundColor = [UIColor clearColor];
        [labelTempDay[i] setTextColor:[UIColor whiteColor]];
        [labelTempDay[i] setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:labelTempDay[i]];
    }
    
    if (self.pointsOfDay.count == 5)
    {
        for (int i = 0; i < self.pointsOfDay.count; i++)
        {
            
            labelTempDay[i].text = [NSString stringWithFormat:@"%@℃",tempOfDays_str[i]];
        }
    }
    else
    {
        for (int i = 0; i < self.pointsOfDay.count; i++)
        {
            
            labelTempDay[i].text = [NSString stringWithFormat:@"%@℃",tempOfDays_str[i + 1]];
        }
    }
    
}


@end














